<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Image;

class UploadController extends Controller
{
    //
    public function editor(Request $request)
    {
        $files = $request->file();
        if ($files) {
//            dump($files);
//            foreach ($files as $file) {
////                $this->saveimage($file);
////                $urls[] = Storage::url($file->store('images/'.date('Y-m-d'),'uploadimg'));
//                $urls[] = $this->saveandthumb($file);
//            }
//            dump($files);
            $ret = $this->saveandthumb($files['file']);
            $ret['code'] = 200;
            return $ret;
//            return response('Hello World', 200)->json($this->saveandthumb($files['file']));
        }

    }

    public function upload(Request $request)
    {
        $files = $request->file();
        if ($files) {
            $ret = $this->saveandthumb($files['img']);
            return $ret;
        }
    }

    public function deletefile(Request $request)
    {
        $dir = config('filesystems.disks.uploadimg.url');
        $name = str_replace($dir,'',$request->input('url'));
        $storage = Storage::disk('uploadimg');
        if($storage->exists($name)){
            $storage->delete($name);
        }
        return true;
    }

    public function getlist()
    {
        $dir = config('filesystems.disks.uploadimg.url');
        dump($dir);
    }


    //Image扩展使用
    private function saveandthumb($image)
    {
//
        $ret = [];
        $size = getimagesize($image);
//        dump($image);
//        dump($size);
        \Illuminate\Http\UploadedFile::class;
        $ret['original'] = $image->getClientOriginalName();
        $ret['size'] = $image->getSize();

//        dump($size);
        $width = $size[0];
        $max_width = config('upload.editor.max_width');
        $extension = $image->getClientOriginalExtension();
        $ret['type'] = '.'.$extension;
        $title = uniqid() . '.' . $extension;
        $filename = date('Y-m-d') . '/' . $title;
        $ret['name'] = $filename;
        if ($width > $max_width) {
            $bili = $width / $max_width;
            $height = $size[1] / $bili;
            $im = Image::make($image)->resize($max_width, $height);
            Storage::disk('uploadimg')->put($filename, $im->encode($extension));

        } else {
            $path = $image->getRealPath();
            Storage::disk('uploadimg')->put($filename, file_get_contents($path));
        }

        $ret['url'] = config('filesystems.disks.uploadimg.url') . '/' . $filename;
        $ret['state'] = 'SUCCESS';
        return $ret;
    }

    //手动设置
//    private function saveimage($image)
//    {
//        //获取文件的原文件名 包括扩展名
//        $yuanname= $image->getClientOriginalName();
//        dump($yuanname);
//
//        //获取文件的扩展名
//        $kuoname=$image->getClientOriginalExtension();
//        dump($kuoname);
//        //获取文件的类型
//        $type=$image->getClientMimeType();
//        dump($type);
//        //获取文件的绝对路径，但是获取到的在本地不能打开
//        $path=$image->getRealPath();
//        dump($path);
//        //要保存的文件名 时间+扩展名
//        $filename=date('Y-m-d') . '/' . uniqid() .'.'.$kuoname;
//        //保存文件          配置文件存放文件的名字  ，文件名，路径
//        $bool= Storage::disk('uploadimg')->put($filename,file_get_contents($path));
//        dump($bool);
//
//    }
}
